package com.training.hatton.dropboxclient.service;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.os.ResultReceiver;

import com.training.hatton.base.RequestDataWrapper;
import com.training.hatton.dropboxclient.content.Metadata;
import com.training.hatton.dropboxclient.processor.common.DropboxListModelProcessor;
import com.training.hatton.dropboxclient.utill.ProcessorHelperFactory;

public class RESTService extends Service {

	private static final int POOL_LIMIT = 3;
	private ExecutorService ex;
	
	@Override
	public void onCreate() {
		ex = Executors.newFixedThreadPool(POOL_LIMIT);
		super.onCreate();
	}
	
	@Override
	public int onStartCommand(Intent intent, int flags, int startId) {
		RequestDataWrapper dataWrapper = new RequestDataWrapper(intent.getBundleExtra(ServiceHelper.EXTRA_DATA_REQUEST));
		ResultReceiver resultReceiver = intent.getParcelableExtra(ServiceHelper.EXTRA_RESULT_RECEIVER);
		final DropboxListModelProcessor<Metadata> processor = ProcessorHelperFactory.getProcessor(dataWrapper.getProcessorId(), dataWrapper, resultReceiver);
		ex.execute(new Runnable() {
			
			@Override
			public void run() {
				processor.start();
			}
		});
		return START_STICKY;
	} 
	
	@Override
	public IBinder onBind(Intent intent) {
		return null;
	}
}